﻿package
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.*;
	
	/** This class creates a skinny menu with "forward" and "back" buttons printed.  
	Generally intended to be left-to-right - i.e., right is forward and when clicked dispatches a COMPLETE event
	left is back and when clicked dispatches a CANCEL event, and is also optional.  If this order is wrong, set leftToRight=false
	*/
	public class ForwardBackMenu extends MovieClip 
	{
		// constants
		private const BORDER_PADDING:int = 10;
		//public vars
		// private objects
		private var fbutton:Sprite, bbutton:Sprite;
		private var ftextField:TextField, btextField:TextField;
		// private vars
		private var WIDTH:int, HEIGHT:int, shortDimension:int, longDimension:int;
		private var buttonhlength:Number;
		private var skinny:Boolean = false;
		private var fenabled:Boolean, benabled:Boolean;
		
		public function ForwardBackMenu(WIDTH:int, HEIGHT:int, flabel:String, fenabled:Boolean=true, blabel:String="", benabled:Boolean=false, leftToRight:Boolean=true)
		{
			
			this.WIDTH = WIDTH;
			this.HEIGHT = HEIGHT;
			this.fenabled = fenabled;
			this.benabled = benabled;
			
			this.graphics.drawRect(0,0,WIDTH,HEIGHT);
			if (WIDTH < HEIGHT)
			{
				skinny = true;
				shortDimension = WIDTH;
				longDimension = HEIGHT;
			} else 
			{
				skinny = false;
				shortDimension = HEIGHT;
				longDimension = WIDTH;
			} 
			// base length of triangle by size of the panel and border padding
			buttonhlength = shortDimension/2 - BORDER_PADDING;
			
			//Create a forward button
			fbutton = new Sprite();
			drawTriangle(fbutton, false);
			ftextField = new TextField();
			ftextField.defaultTextFormat = new TextFormat("arial", 20, 0x000000);
			ftextField.selectable = false;
			ftextField.text = flabel;
			
			
			// Place correctly
			if (leftToRight)
			{
				// Forward at bottom
				if (skinny)
				{
					fbutton.x = (this.WIDTH)/2;
					fbutton.y = this.HEIGHT - fbutton.height/2 - BORDER_PADDING;
					fbutton.rotation = 180;
					ftextField.width = shortDimension - 2*BORDER_PADDING;
					ftextField.autoSize = "left";
					ftextField.x = BORDER_PADDING;
					ftextField.y = fbutton.y - ftextField.height/2 - BORDER_PADDING;
				} else
				{ //Forward at right
					fbutton.x = this.WIDTH - fbutton.width/2 - BORDER_PADDING;
					fbutton.y = (this.HEIGHT) / 2;
					fbutton.rotation = 90;
					ftextField.autoSize = "left";
					ftextField.x = fbutton.x - fbutton.width/2 - ftextField.width - BORDER_PADDING;
					ftextField.y = (this.HEIGHT - ftextField.height) / 2;
				}
			} else
			{
				// Forward at top
				if (skinny)
				{
					fbutton.x = (this.WIDTH)/2;
					fbutton.y = BORDER_PADDING;
					fbutton.rotation = 0;
					ftextField.width = shortDimension - 2*BORDER_PADDING;
					ftextField.autoSize = "left";
					ftextField.x = BORDER_PADDING;
					ftextField.y = fbutton.y + fbutton.height + BORDER_PADDING;
				} else
				{ //Forward at left
					fbutton.x = BORDER_PADDING;
					fbutton.y = (this.HEIGHT - fbutton.height) / 2;
					fbutton.rotation = -90;
					ftextField.autoSize = "left";
					ftextField.x = fbutton.x + fbutton.width + BORDER_PADDING;
					ftextField.y = (this.HEIGHT - ftextField.height) / 2;
				}
			}
			// enable and add
			if (fenabled)
			{
				fbutton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverForward);
			} else
			{
				fbutton.alpha = .25;
			}
			this.addChild(fbutton);
			this.addChild(ftextField);
			
			//Create a reverse button
			if (blabel.length > 0)
			{
				bbutton = new Sprite();
				drawTriangle(bbutton, false);
				btextField = new TextField();
				btextField.defaultTextFormat = new TextFormat("arial", 20, 0x000000);
				btextField.selectable = false;
				btextField.text = blabel;
								
				// Place correctly, opposite of above.
				if (!leftToRight)
				{
					// Back at bottom
					if (skinny)
					{
						bbutton.x = (this.WIDTH)/2;
						bbutton.y = this.HEIGHT - bbutton.height - BORDER_PADDING;
						bbutton.rotation = 180;
						btextField.width = shortDimension - 2*BORDER_PADDING;
						btextField.autoSize = "left";
						btextField.x = BORDER_PADDING;
						btextField.y = bbutton.y - btextField.height - BORDER_PADDING;
					} else
					{ //Back at right
						bbutton.x = this.WIDTH - bbutton.width - BORDER_PADDING;
						bbutton.y = (this.HEIGHT - bbutton.height) / 2;
						bbutton.rotation = 90;
						btextField.autoSize = "left";
						btextField.x = bbutton.x - btextField.width - BORDER_PADDING;
						btextField.y = (this.HEIGHT - btextField.height) / 2;
					}
				} else
				{
					// Back at top
					if (skinny)
					{
						bbutton.x = (this.WIDTH)/2;
						bbutton.y = BORDER_PADDING;
						bbutton.rotation = 0;
						btextField.width = shortDimension - 2*BORDER_PADDING;
						btextField.autoSize = "left";
						btextField.x = BORDER_PADDING;
						btextField.y = bbutton.y + bbutton.height + BORDER_PADDING;
					} else
					{ //Back at left
						bbutton.x = BORDER_PADDING + bbutton.width/2;
						bbutton.y = (this.HEIGHT) / 2;
						bbutton.rotation = -90;
						btextField.autoSize = "left";
						btextField.x = bbutton.x + bbutton.width/2 + BORDER_PADDING;
						btextField.y = (this.HEIGHT - btextField.height) / 2;
					}
				}
				// enable and add
				if (benabled)
				{
					bbutton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverBack);
				}  else
				{
					bbutton.alpha = .25;
				}
				this.addChild(bbutton);
				this.addChild(btextField);
			}
		}
	
		/******** FORWARD BUTTON INTERACTIVITY ****************/
		/** Enable */
		public function enableForward():void
		{
			if (!fenabled)
			{
				fbutton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverForward);
				fbutton.alpha = 1;
			}
		}
		public function disableForward():void
		{
			if (fenabled)
			{
				fbutton.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverForward);
				fbutton.alpha = .25;
			}			
		}
		/** Mouse over hightlights the button and adds click handler */
		public function handleMouseOverForward (event:MouseEvent):void
		{
			if (fenabled)
			{
				fbutton.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverForward);
				drawTriangle(fbutton, true);
				fbutton.addEventListener(MouseEvent.CLICK, handleClickForward);
				fbutton.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOutForward);
			} else {
				trace ("Forward button needs to be enabled");
			}
		}
		public function handleClickForward (event:MouseEvent):void
		{
			fbutton.removeEventListener(MouseEvent.CLICK, handleClickForward);
			drawTriangle(fbutton, false);
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		public function handleMouseOutForward (event:MouseEvent):void
		{
			fbutton.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutForward);
			fbutton.removeEventListener(MouseEvent.CLICK, handleClickForward);
			drawTriangle(fbutton, false);
			fbutton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverForward);
		}
		

		/******** BACK BUTTON INTERACTIVITY ****************/
		/** Enable */
		public function enableBack():void
		{
			if (!fenabled)
			{
				bbutton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverBack);
				bbutton.alpha = 1;
			}
		}
		public function disableBack():void
		{
			if (fenabled)
			{
				bbutton.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverBack);
				bbutton.alpha = .25;
			}			
		}
		/** Mouse over hightlights the button and adds click handler */
		public function handleMouseOverBack (event:MouseEvent):void
		{
			if (fenabled)
			{
				bbutton.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverBack);
				drawTriangle(bbutton, true);
				bbutton.addEventListener(MouseEvent.CLICK, handleClickBack);
				bbutton.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOutBack);
			} else {
				trace ("Back button needs to be enabled");
			}
		}
		public function handleClickBack (event:MouseEvent):void
		{
			bbutton.removeEventListener(MouseEvent.CLICK, handleClickBack);
			this.dispatchEvent(new Event(Event.CANCEL));
			bbutton.graphics.clear();
			drawTriangle(bbutton, false);
			this.dispatchEvent(new Event(Event.CANCEL));
			//bbutton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverBack);
		}
		public function handleMouseOutBack (event:MouseEvent):void
		{
			bbutton.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutBack);
			bbutton.removeEventListener(MouseEvent.CLICK, handleClickBack);
			drawTriangle(bbutton, false);
			bbutton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverBack);
		}
		/** Used to draw either the forward or back button */
		private function drawTriangle(b:Sprite, withHeavyOutline:Boolean=false):void
		{
			b.graphics.clear();
			if (withHeavyOutline)
			{
				b.graphics.lineStyle(4,0x000000);
			} else
			{
				b.graphics.lineStyle(2,0x888888);
			}
			b.graphics.beginFill(0xFF8800);
			b.graphics.moveTo(0, -buttonhlength);
			b.graphics.lineTo(-buttonhlength, buttonhlength);
			b.graphics.lineTo(buttonhlength, buttonhlength);
			b.graphics.lineTo(0, -buttonhlength);			
		}
	}
}